package com.k2data.k2app.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.k2data.k2app.common.MyConstant;
import com.k2data.k2app.domain.EventCond;
import com.k2data.k2app.domain.EventDTO;
import com.k2data.k2app.domain.po.DictPO;
import com.k2data.k2app.domain.po.EventCustomer;
import com.k2data.k2app.domain.po.v1.Event;
import com.k2data.k2app.domain.po.v1.RFile;
import com.k2data.k2app.exception.K2ResponseException;
import com.k2data.k2app.mapper.EventCustomerMapper;
import com.k2data.k2app.mapper.EventMapper;
import com.k2data.k2app.mapper.REventPointMapper;
import com.k2data.k2app.mapper.RFileMapper;
import com.k2data.k2app.rest.InstanceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lidong9144@163.com 17-3-20.
 */
@Service
public class EventV1Service {

    private final EventMapper eventMapper;


    @Autowired
    private InstanceClient instanceClient;
    @Autowired
    private RFileMapper rFileMapper;
    @Autowired
    private REventPointMapper rEventPointMapper;
    @Autowired
    private ConditionService conditionService;
    @Autowired
    public EventV1Service (EventMapper eventMapper) {
        this.eventMapper = eventMapper;
    }

    /**
     * 插入客户事件
     *
     * @param po 实体
     * @return 插入 id
     * @throws K2ResponseException 客户 id 没找到, eventType 没找到
     */
    @Transactional
    public Long insertEvent(Event po) throws K2ResponseException {
        po.setFaultInfoValue(po.getType().equals("fault")?po.getFaultInfoValue():null);
        eventMapper.insertSelective(po);
        if(po.getEventPonts() != null){
            po.getEventPonts().forEach(s->{
                s.setEventId(po.getId());
            });
            rEventPointMapper.insertBatch(po.getEventPonts());
        }
        List<RFile> rFiles = RFile.updateFiles(po.getId(),po.getFileList(),"event");
        if(rFiles!=null && rFiles.size()>0){
            rFileMapper.insertBatch(rFiles);
        }
        return po.getId();
    }

    @Transactional
    public int updateEvent(Event po) throws K2ResponseException {
        rFileMapper.deleteByEventId(po.getId(),"event");
        List<RFile> rFiles = RFile.updateFiles(po.getId(),po.getFileList(),"event");
        if(rFiles!=null && rFiles.size()>0){
            rFileMapper.insertBatch(rFiles);
        }
        po.setFaultInfoValue(po.getType().equals("fault")?po.getFaultInfoValue():null);
        return eventMapper.updateByPrimaryKey(po);
    }


    public Event list(Event cond) throws InterruptedException {
        List<DictPO> typeList = Lists.newArrayList();
        //目前有的所有故障类型
        List<String> types = eventMapper.selectTypes();
        //所有的故障类型
        List eventType = Lists.newArrayList("eventType");
        Map<String, List<DictPO>> typeAll = conditionService.getCondition(eventType);
        List<DictPO> eventType1 = typeAll.getOrDefault("eventType",new ArrayList());
        if(eventType1.size()>0){
            eventType1.forEach(s->{
                if (types != null && types.contains(s.getValue())) {
                    typeList.add(s);
                }
            });
        }
        List<Event> eventList = getAllEventList(cond);
        Event dto = new Event();
        // 显示所有的故障类型
//        dto.setTypes(typeList);
        dto.setTypes(eventType1);
        dto.setEventList(eventList);
        return dto;
    }

    public List<Event> getAllEventList(Event cond) {

        List<Event> eventList = eventMapper.selectList1(cond);
        for(Event event:eventList){
            //为所有文件设置文件路径
            if(event.getFileList() != null && event.getFileList().size()>0){
                List<RFile> byCustomerId = event.getFileList();
                for(RFile rFile:byCustomerId){
                    //设置文件路径
                    rFile.setFileUrl(instanceClient.getGatewayUrl() + Paths.get("/static/", rFile.getFileUrl()).toString());
                }
                event.setFileList(byCustomerId);
            }
        }
        return eventList;
    }

    public List<Event> faultBarChart(Event cond) {
        List<Event> list = Lists.newArrayList();

        for (Event eventFault : eventMapper.selectTypeCount(cond)) {
            Event dto = new Event();
            dto.setEventType(eventFault.getEventType());
            dto.setCount(eventFault.getCount());

            list.add(dto);
        }

        return list;
    }

    public List<Event> selectFaultCount(Event cond) {
        List<Event> list = Lists.newArrayList();

        for (Event eventFault : eventMapper.selectFaultCount(cond)) {
            Event dto = new Event();
            dto.setFaultName(eventFault.getEventType());
            dto.setCount(eventFault.getCount());
            dto.setDeviceCount(eventFault.getDeviceCount());
            list.add(dto);
        }

        return list;
    }

    public List<RFile> getFileUrls(Long id) {
        List<RFile> byCustomerId = rFileMapper.getByEventId(id,"event");
        for(RFile rFile:byCustomerId){
            rFile.setFileUrl(instanceClient.getGatewayUrl() + Paths.get("/static/", rFile.getFileUrl()).toString());
        }
        return byCustomerId;
    }



/*    public List<EventDTO> customerTypeCount(EventCond cond) {
        List<EventDTO> list = Lists.newArrayList();

        for (EventCustomer eventFault : eventCustomerMapper.selectTypeCount(cond)) {
            EventDTO dto = new EventDTO();
            dto.setEventType(eventFault.getEventType());
            dto.setCount(eventFault.getCount());

            list.add(dto);
        }

        return list;
    }*/
}
